(function ($) {
	var ipPartStart = false; //用于切换IPPart后的信息录入
	mui('body').on('tap','#configBtn',function(){
		mui.showIpConfig()
	})
	$.showIpConfig = function(type){
		var configPopo = $('#configPopo')[0]
		if (configPopo) {
			var configDict = localStorage['ipConfigDict'];
			if (configDict) {
				configDict = JSON.parse(localStorage['ipConfigDict']);
				updateUIWithDefault(configDict)
			}else{
				updateUIWithDefault()
			}
			configPopo.classList.add('mui-active')
			return
		}
		configPopo = document.createElement('div')
		document.body.appendChild(configPopo);
		configPopo.id = 'configPopo'
		configPopo.setAttribute('class','configPopo')
		configPopo.innerHTML=getPopoContentHtml();
		
		setupConfigTapped();
		var configDict = localStorage['ipConfigDict'];
		if (configDict) {
			configDict = JSON.parse(localStorage['ipConfigDict']);
			updateUIWithDefault(configDict)
		}else{
			updateUIWithDefault()
		}
		configPopo.classList.add('mui-active')
	}
	function updateUIWithDefault(dict){
		var tempIp = ipUrl
		var tempPro = protocol
		var temp85 = parseInt(isHttps)
		var tempWeb= webUrl
		if (dict) {
			tempPro = dict['protocol']
			temp85 = parseInt(dict['isHttps'])
			tempWeb = dict['webUrl']
			tempIp = dict['ipUrl']
		}
		var hisStr = '.conRight.hisCell .option[data-index="1"]'
		if (temp85 === 1) {
			hisStr = '.conRight.hisCell .option[data-index="0"]'
		}
		mui.trigger(mui(hisStr)[0],'tap')
		//协议
		var cateStr = '.conRight.cateCell .option[data-index="0"]'
		if (tempPro == "https"+':') {
			cateStr = '.conRight.cateCell .option[data-index="1"]'
		}
		mui.trigger(mui(cateStr)[0],'tap')
		
		var webStr = '.conRight.webCell .option[data-index="1"]'
		if (tempWeb == "/imedical/webservice") {
			webStr = '.conRight.webCell .option[data-index="2"]'
		}else if (tempWeb == "/dthealth/web") {
			webStr = '.conRight.webCell .option[data-index="0"]'
		}
		mui.trigger(mui(webStr)[0],'tap')
		
		//ip
		var ipCellArr = tempIp.split(":")
		mui('#IPAddress')[0].value = (ipCellArr[0]||"")
		mui('#port')[0].value = (ipCellArr[1]||"")
	}
	function saveConfigToLocal(){
		var tempIP = mui('#IPAddress')[0].value
		var tempPort = mui('#port')[0].value
		if(!checkIP(tempIP) || !checkPort(tempPort)) {
			return
		}
		if (tempPort!="") {
			tempIP = tempIP + ':'+tempPort
		}
		var cateValue = mui('.conRight.cateCell .option.mui-active')[0].innerText+':'
		var webValue = mui('.conRight.webCell .option.mui-active')[0].innerText
		var hisDom = mui('.conRight.hisCell .option.mui-active')[0]
		var hisValue = 1
		if (hisDom == undefined || hisDom.getAttribute('data-index') == '1') {
			hisValue = 0
		}
		var configDict = {'protocol':cateValue,'isHttps':hisValue,'webUrl':'/'+webValue,'ipUrl':tempIP};
		localStorage['ipConfigDict'] = JSON.stringify(configDict);
		closeConfigPopo()
		mui.toast('设置成功');
	}
	function setupConfigTapped(){
		mui('.popoContent').on('tap','#configClose',closeConfigPopo)
		mui('li').on('tap','.option',function(){
			var conRight = this.parentNode
			var oldActive = conRight.querySelector('.mui-active')
			if (oldActive && oldActive!= this) {
				oldActive.classList.remove('mui-active')
			}
			this.classList.add('mui-active')
			if (conRight.getAttribute('class').indexOf('hisCell') == -1) { //hisCell
				return
			}
			var index = this.getAttribute('data-index')
			if (index == 0) {
				mui.trigger(mui('.conRight.cateCell .option[data-index="1"]')[0],'tap')
				mui.trigger(mui('.conRight.webCell .option[data-index="2"]')[0],'tap')
			}else{
				mui.trigger(mui('.conRight.cateCell .option[data-index="0"]')[0],'tap')
				if (mui('.conRight.webCell .option[data-index="2"].mui-active')[0]) {
					mui.trigger(mui('.conRight.webCell .option[data-index="1"]')[0],'tap')
				}
			}
		});
		
		mui('.popoContent').on('tap','.resetBtn',function(){ //恢复默认
			updateUIWithDefault();
		})
		mui('.popoContent').on('tap','.confirmBtn',saveConfigToLocal) //确认地址
	}
	
	function closeConfigPopo(){
		$('#configPopo')[0].classList.remove('mui-active')	
	}
	
	function getPopoContentHtml(){
		var header = '<div class="headTip nurfont nur-setting">服务器地址及端口号配置<div id="configClose" class="mui-icon mui-icon-closeempty"></div></div>'
		var cateCell = '<li class="mui-table-view-cell cellFlex">'+
			'<div class="tip">类型：</div>'+
			'<div class="conRight cateCell">'+
				'<div class="option nurfont nur-correct" data-index="0">http</div>'+
				'<div class="option nurfont nur-correct" data-index="1">https</div>'+
			'</div></li>'
		var ipCell = '<li class="mui-table-view-cell">'+
			'<form class="mui-input-group">'+
				'<div class="mui-input-row cellFlex">'+
					'<div class="tip">IP：</div>'+
					'<input id="IPAddress" type="text" class="conRight mui-input mui-input-clear" placeholder="192.168.1.101">'+
				'</div>'+
			'</form></li>'
		var portCell = '<li class="mui-table-view-cell">'+
			'<form class="mui-input-group">'+
				'<div class="mui-input-row cellFlex">'+
					'<div class="tip">端口：</div>'+
					'<input id="port" type="number" class="mui-input-clear mui-input" placeholder="0~65535">'+
				'</div>'+
			'</form></li>'
		var webCell = '<li class="mui-table-view-cell cellFlex">'+
			'<div class="tip">Web：</div>'+
			'<div id="webOptions" class="conRight webCell">'+
				'<div class="option nurfont nur-correct" data-index="0">dthealth/web</div><br>'+
				'<div class="option nurfont nur-correct" data-index="1">imedical/web</div><br>'+
				'<div class="option nurfont nur-correct" data-index="2">imedical/webservice</div>'+
			'</div></li>'
		var his = '<li class="mui-table-view-cell cellFlex">'+
			'<div class="tip">HIS：</div>'+
			'<div class="conRight hisCell">'+
				'<div class="option nurfont nur-correct" data-index="0">8.5及以后版本</div>'+
				'<div class="option nurfont nur-correct" data-index="1">8.4及以前版本</div>'+
			'</div></li>'
		var html='<div class="popoContent">'+header+
				'<ul class="mui-table-view ProxyHISType">'+
					'<li class="mui-table-view-cell cellFlex">'+
						'<div class="tip">网络类型：</div>'+
						'<div id="proxyHISCate" class="conRight proxyHISCell">'+
							'<div class="option nurfont nur-correct" data-index="0">代理</div><br>'+
							'<div class="option nurfont nur-correct" data-index="1">内网HIS</div><br>'+
						'</div></li>'+
						'<li class="mui-table-view-cell cellFlex">'+
							'<div class="tip">网络类型：</div><span>代理</span>'+
							'<div class="mui-switch mui-switch-blue mui-switch-mini">'+
								'<div class="mui-switch-handle"></div>'+
							'</div><span>内网HIS</span>'+
							'</li>'+
						
				'</ul>'+
				'<ul class="mui-table-view HisMode">'+cateCell+ipCell+portCell+webCell+his+'</ul>'+
				'<ul class="mui-table-view ProxyMode">'+getProxyHtml()+'</ul>'+
				'<div class="bottomBtns">'+
					'<button class="resetBtn">恢复默认</button>'+
					'<button class="confirmBtn">确认地址</button>'+
				'</div>'+
			'</div>'
		return html
	}
	function getProxyHtml(){
		var html = '<li class="mui-table-view-cell">'+
			'<form class="mui-input-group">'+
				'<div class="mui-input-row cellFlex">'+
					'<div class="tip">地址：</div>'+
					'<input id="ProxyAddress" type="text" class="conRight mui-input mui-input-clear" placeholder="http://192.168.1.101/get">'+
				'</div>'+
			'</form></li>'
		return html
	}
	//检查地址、端口合法性
	function checkIP(ipstr) {
		return true;
		var exp = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
		var reg = ipstr.match(exp);
		if(reg == null) {
			mui.toast('IP地址不合法');
			return false;
		} else {
			return true;
		}
	}
	
	function checkPort(portstr) {
		if(portstr && portstr !="") {
			if (portstr.length <= 5 && parseInt(portstr) > 0 && parseInt(portstr) < 65536) {
				return true;
			}else{
				mui.toast('端口号不合法');
				return false
			}
		} else {
			return true;
		}
	}
})(mui)


